package com.qyhl.webtv.module_news.news.jlnews.multi;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.entity.news.JLCityBean;
import com.qyhl.webtv.commonlib.utils.vertical.QTabView;
import com.qyhl.webtv.commonlib.utils.vertical.TabAdapter;
import com.qyhl.webtv.commonlib.utils.vertical.TabView;
import com.qyhl.webtv.commonlib.utils.vertical.VerticalTabLayout;
import com.qyhl.webtv.commonlib.utils.vertical.VerticalViewPager;
import com.qyhl.webtv.module_news.news.jlnews.multi.fragment.JLMultiNewsItemFragment;
import com.qyhl.webtv.module_news.news.union.town2.Town2ListPagerAdapter;
import com.weavey.loading.lib.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class JLMultiNewsFragment extends BaseFragment implements JLMultiNewsContract.JLMultiNewsView {
    @BindView(R2.id.tablayout)
    VerticalTabLayout tablayout;
    @BindView(R2.id.verticalviewpager)
    VerticalViewPager verticalviewpager;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;


    private View view;

    private JLMultiNewsPresenter mPresenter;

    private List<String> titleList;

    private List<Fragment> fragmentList;

    private static final float MIN_SCALE = 0.75f;
    private static final float MIN_ALPHA = 0.75f;


    public static JLMultiNewsFragment newInstance() {
        return new JLMultiNewsFragment();
    }


    /**
     * 加载数据
     */
    private void loadData() {
        mPresenter.getCityList();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
        }
    }


    @Override
    public void initImmersionBar() {
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.news_fragment_jlmulti_list, null);
    }

    @Override
    protected void create() {
        mPresenter = new JLMultiNewsPresenter(this);
        //下拉刷新
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {
        loadData();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        fragmentList = new ArrayList<>();
        titleList = new ArrayList<>();
    }

    private void setListeners() {
        //重载
        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getCityList();
        });

        tablayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                verticalviewpager.setCurrentItem(position);
                for (int i = 0; i < titleList.size(); i++) {
                    if (i == position) {
                        tab.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
                    } else {
                        tablayout.getTabAt(i).setBackgroundColor(ContextCompat.getColor(getContext(), R.color.global_background));
                    }
                }

            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });

        verticalviewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tablayout.setTabSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void setCityList(List<JLCityBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        fragmentList.clear();
        for (int i = 0; i < beanList.size(); i++) {
            titleList.add(beanList.get(i).getCity());
            fragmentList.add(JLMultiNewsItemFragment.newInstance(beanList.get(i).getId()));
        }

        tablayout.setTabAdapter(new MyTabAdapter(titleList));
        try {
            tablayout.getTabAt(0).setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
        } catch (Exception e) {
            e.printStackTrace();
        }

        Town2ListPagerAdapter mPagerAdapter = new Town2ListPagerAdapter(getActivity().getSupportFragmentManager(), fragmentList);
        mPagerAdapter.setData(titleList);
        verticalviewpager.setAdapter(mPagerAdapter);
        verticalviewpager.setPageMargin(getResources().
                getDimensionPixelSize(R.dimen.news_pagemargin));
        verticalviewpager.setPageMarginDrawable(new ColorDrawable(
                ContextCompat.getColor(getContext(), R.color.white)));


        verticalviewpager.setPageTransformer(true, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View view, float position) {
                int pageWidth = view.getWidth();
                int pageHeight = view.getHeight();

                if (position < -1) { // [-Infinity,-1)
                    // This page is way off-screen to the left.
                    view.setAlpha(0);

                } else if (position <= 1) { // [-1,1]
                    // Modify the default slide transition to shrink the page as well
                    float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                    float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                    float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                    if (position < 0) {
                        view.setTranslationY(vertMargin - horzMargin / 2);
                    } else {
                        view.setTranslationY(-vertMargin + horzMargin / 2);
                    }

                    // Scale the page down (between MIN_SCALE and 1)
                    view.setScaleX(scaleFactor);
                    view.setScaleY(scaleFactor);

                    // Fade the page relative to its size.
                    view.setAlpha(MIN_ALPHA +
                            (scaleFactor - MIN_SCALE) /
                                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

                } else { // (1,+Infinity]
                    // This page is way off-screen to the right.
                    view.setAlpha(0);
                }
            }
        });

    }

    @Override
    public void setError() {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setErrorText("暂无内容或获取失败！");
    }

    class MyTabAdapter implements TabAdapter {

        List<String> titles;

        public MyTabAdapter(List<String> titles) {
            this.titles = titles;
        }

        public void setTitles(List<String> titles) {
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public int getBadge(int position) {
            return 0;
        }

        @Override
        public QTabView.TabIcon getIcon(int position) {
            return null;
        }

        @Override
        public QTabView.TabTitle getTitle(int position) {
            return new QTabView.TabTitle.Builder(getContext())
                    .setContent(titles.get(position))
                    .setTextColor(getResources().getColor(R.color.global_base), getResources().getColor(R.color.global_black_lv1))
                    .build();
        }

        @Override
        public int getBackground(int position) {
            return 0;
        }
    }

}
